package com.arpa.ntocc.common.common;

import cn.jpush.api.utils.StringUtils;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;

@Service
public class CustomObjectMapper extends ObjectMapper {
    private static final long serialVersionUID = 5443210148021512090L;

    public CustomObjectMapper() {
        this.getSerializerProvider().setNullValueSerializer(new NullSerializer());
        this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        this.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
        this.configure(SerializationFeature.INDENT_OUTPUT, true);
        this.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, false);
        this.setPropertyNamingStrategy(new CamelCaseStrategy());
    }

    public CustomObjectMapper(String wrapRootValue) {
        this.getSerializerProvider().setNullValueSerializer(new NullSerializer());
        this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        if (!StringUtils.isEmpty(wrapRootValue) && "yes".equals(wrapRootValue)) {
            this.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
        }

        this.configure(SerializationFeature.INDENT_OUTPUT, true);
        this.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, false);
        this.setPropertyNamingStrategy(new CamelCaseStrategy());
    }

    public class CamelCaseStrategy extends PropertyNamingStrategy.PropertyNamingStrategyBase {
        public CamelCaseStrategy() {
        }

        @Override
        public String translate(String input) {
            if (input == null) {
                return input;
            } else if (input != null && !input.isEmpty()) {
                if (!input.contains("_")) {
                    return input.substring(0, 1).toLowerCase() + input.substring(1);
                } else {
                    int length = input.length();
                    StringBuilder result = new StringBuilder(length * 2);
                    String[] camels = input.split("_");
                    String[] arr$ = camels;
                    int len$ = camels.length;

                    for(int i$ = 0; i$ < len$; ++i$) {
                        String camel = arr$[i$];
                        if (!camel.isEmpty()) {
                            if (result.length() == 0) {
                                result.append(camel.toLowerCase());
                            } else {
                                result.append(camel.substring(0, 1).toUpperCase());
                                result.append(camel.substring(1).toLowerCase());
                            }
                        }
                    }

                    return result.toString();
                }
            } else {
                return "";
            }
        }
    }

    private class NullSerializer extends JsonSerializer<Object> {
        private NullSerializer() {
        }

        @Override
        public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            if (!(value instanceof Double) && !(value instanceof Float) && !(value instanceof Integer)) {
                jgen.writeString("");
            } else {
                jgen.writeString("0");
            }

        }
    }
}
